package com.piece.core.web.authority.store;

import com.piece.core.web.properties.SecurityProperties;
import com.piece.core.web.authority.store.impl.AuthenticationKeyGeneratorImpl;
import com.piece.core.web.authority.store.impl.RedisTokenStoreImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.oauth2.provider.token.TokenStore;
import javax.annotation.Resource;

/**
 * 认证服务器使用Redis存取令牌
 */
@Configuration
@ConditionalOnProperty(prefix = "spring.security.token", name = "store-type", havingValue = "redis")
public class AuthRedisTokenStore {

    @Resource
    private SecurityProperties securityProperties;

    @Resource
    private RedisTemplate redisTemplate;

    @Bean
    public TokenStore tokenStore() {
        RedisTokenStoreImpl redisTokenStore = new RedisTokenStoreImpl(redisTemplate, securityProperties);
        redisTokenStore.setAuthenticationKeyGenerator(new AuthenticationKeyGeneratorImpl());
        return redisTokenStore;
    }
}
